package kr.ac.kongju.gedl.admin.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;


import kr.ac.kongju.gedl.attach.vo.AttachFileVO;
import kr.ac.kongju.gedl.board.service.BoardContentsService;
import kr.ac.kongju.gedl.board.vo.ContentsVO;
import kr.ac.kongju.gedl.code.service.CodeService;
import kr.ac.kongju.gedl.code.vo.CodeInfoVO;
import kr.ac.kongju.gedl.common.util.ListResultVO;
import kr.ac.kongju.gedl.item.service.ItemService;
import kr.ac.kongju.gedl.item.vo.ItemInfoVO;
import kr.ac.kongju.gedl.item.vo.ItemVO;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartRequest;


import egovframework.rte.fdl.property.EgovPropertyService;

@Controller
public class CodeAdminController {
	
	/** EgovPropertyService */
    @Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
    
    @Resource(name = "codeService")
    protected CodeService codeService;
    
    private static final String DEFAULT_SORT = "error_report_id";
    private static final String DEFAULT_DIR = "desc";
	
    private final Logger logger = LoggerFactory.getLogger(getClass());
    
    
    @RequestMapping(value="/admin/codeUpload.do")
	public String upload(HttpServletRequest request, 
    		ModelMap model, SessionStatus status) throws Exception {
		
    	CodeInfoVO codeInfoVO = new CodeInfoVO();
    	
    	model.addAttribute("itemVO", codeInfoVO);
    	
		return "/admin/codeWrite";
	}
    
    @RequestMapping(value="/admin/uploadCodeProcess.do", method = RequestMethod.POST)
	public String uploadProcess(HttpServletRequest request,ModelMap model, SessionStatus status) throws Exception {
    	
    	if (request instanceof MultipartHttpServletRequest) {
    	
    		MultipartFile file = ((MultipartHttpServletRequest)request).getFile("excelFile");
        	
        	BufferedInputStream bis = new BufferedInputStream(file.getInputStream());
        	
        	Workbook wb = WorkbookFactory.create(file.getInputStream());
        	
        	int sheetCount = wb.getNumberOfSheets();
        	for(int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++){
        		org.apache.poi.ss.usermodel.Sheet sheet = wb.getSheetAt(sheetIndex);
            	org.apache.poi.ss.usermodel.Header header = sheet.getHeader();
            	
            	int rowsCount = sheet.getLastRowNum();
                System.out.println("Total Number of Rows: " + (rowsCount + 1));
                for (int i = 1; i <= rowsCount; i++) {
                CodeInfoVO codeInfoVO = new CodeInfoVO();
                org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
                if(sheetIndex != 2) {
                	codeInfoVO.setParent(row.getCell(0).getStringCellValue()); 
                	codeInfoVO.setCodeName(row.getCell(1).getStringCellValue());
                	codeInfoVO.setCodeId(row.getCell(2).getStringCellValue()); 
                	codeInfoVO.setLeafYN("N");
                } else {
                	CodeInfoVO searchCondition = new CodeInfoVO();
                	searchCondition.setParent(row.getCell(0).getStringCellValue());
                	searchCondition.setCodeName(row.getCell(1).getStringCellValue());
                	CodeInfoVO parent = codeService.retrieveCodeInfo(searchCondition);
                	
                	codeInfoVO.setParent(parent.getCodeId());
                	int codeCount = codeService.retrieveListCountCodeInfo(codeInfoVO)+1;
                	
                	codeInfoVO.setCodeName(row.getCell(2).getStringCellValue());
                	codeInfoVO.setCodeId(parent.getCodeId()+String.format("%03d", codeCount)); 
                	codeInfoVO.setLeafYN("Y");
                }
                
                codeInfoVO.setUseYN("Y");
            	codeInfoVO.setCodeOrder(i);
            	
                codeService.registerCodeInfo(codeInfoVO);
                }
                
        	}
        	


    	}

    	ItemVO itemVO = new ItemVO();
    	
    	model.addAttribute("itemVO", itemVO);
    	
		
		return "/admin/itemWrite";
	}
    
}
